<template>
    <!-- 企业资料弹出对话框 -->
    <el-dialog title="企业信息" :visible.sync="loading" width="1430px" top="5vh" append-to-body>

    <!-- 列表 -->
      <el-row>
        <el-table :data="enterpriseList" @selection-change="handleSelectionChange" :header-cell-style="{'text-align':'center'}">
          <el-table-column type="selection" width="55" align="center" v-if="false"/>
          <el-table-column
            v-if="false"
            label="序号"
            type="index"
            min-width="20%"
            align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="企业ID" align="center" prop="enteId" />
          <el-table-column label="企业名称" align="left" prop="enteTitle" width="220"/>
          <el-table-column label="统一社会信用代码" align="left" prop="enteCreditCode" width="150"/>
          <el-table-column label="营业执照" align="left" prop="enteBusinessLicense" width="150">
            <template slot-scope="scope">
              <imageUpload :value="scope.row.enteBusinessLicense"/>
            </template>
          </el-table-column>
          <el-table-column label="邮箱" align="center" prop="enteEmail" width="150"/>
          <el-table-column label="所在地区" align="center" prop="enteArea" :formatter="enteAreaFormat"/>
          <el-table-column label="国家级小巨人企业" align="center" prop="enteIsNationalGiant" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsNationalGiant"/>
            </template>
          </el-table-column>
          <el-table-column label="省级专精特新企业" align="center" prop="enteIsProvinceSpecial" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsProvinceSpecial"/>
            </template>
          </el-table-column>
          <el-table-column label="国家级小型微型企业创业创新示范基地" align="center" prop="enteIsNationalInnovateExampleBase" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsNationalInnovateExampleBase"/>
            </template>
          </el-table-column>
          <el-table-column label="省级小型微型企业创业创新示范基地" align="center" prop="enteIsProvinceInnovateExampleBase" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsProvinceInnovateExampleBase"/>
            </template>
          </el-table-column>
          <el-table-column label="国家级中小企业公共服务示范平台" align="center" prop="enteIsNationalPublicExamplePlatform" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsNationalPublicExamplePlatform"/>
            </template>
          </el-table-column>
          <el-table-column label="省级中小企业公共服务示范平台" align="center" prop="enteIsProvincePublicExamplePlatform" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsProvincePublicExamplePlatform"/>
            </template>
          </el-table-column>
          <el-table-column label="上市公司" align="center" prop="enteIsIpo" min-width="50%">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enteIsIpo"/>
            </template>
          </el-table-column>
          <el-table-column label="审批状态" align="center" prop="enteState" min-width="50%" v-if="false">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_verify_type" :value="scope.row.enteState"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="enteCreateTime" width="120" v-if="false">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.enteCreateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建用户" align="center" prop="enteCreateBy" v-if="false"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleShow(scope.row)"
                v-hasPermi="['business:enterprise:query']"
              >查看更多</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 翻页控件 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

      <!-- 查看或审阅企业对话框 -->
      <el-dialog :title="title" :visible.sync="showopen" width="600px" append-to-body>
        <el-form ref="showForm" :model="showForm" label-width="260px">
          <el-form-item label="企业ID" prop="enteId" v-show="false">
            <el-input v-model="showForm.enteId" placeholder="请输入企业ID" />
          </el-form-item>
          <el-form-item label="企业名称" prop="enteTitle">
            <el-input v-model="showForm.enteTitle" placeholder="请输入企业名称" readonly/>
          </el-form-item>
          <el-form-item label="统一社会信用代码" prop="enteCreditCode">
            <el-input v-model="showForm.enteCreditCode" placeholder="请输入统一社会信用代码" readonly/>
          </el-form-item>
          <el-form-item label="营业执照" prop="enteBusinessLicense">
            <imageUpload v-model="showForm.enteBusinessLicense"/>
          </el-form-item>
          <el-form-item label="邮箱" prop="enteEmail">
            <el-input v-model="showForm.enteEmail" placeholder="请输入邮箱" readonly/>
          </el-form-item>
          <el-form-item label="所在地区" prop="enteArea">
            <treeselect v-model="showForm.enteArea" :options="areaOptions" :normalizer="areaNormalizer" placeholder="请选择所在地区"  :disable-branch-nodes="true"  :default-expand-level="1" disabled/>
          </el-form-item>
          <el-form-item label="地区说明" prop="enteAreaInfo">
            <el-input v-model="showForm.enteAreaInfo" placeholder="请输入地区说明" readonly/>
          </el-form-item>
          <el-form-item label="国家级小巨人企业" prop="enteIsNationalGiant">
            <el-radio-group v-model="showForm.enteIsNationalGiant" disabled>
              <el-radio
                v-for="dict in enteIsNationalGiantOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="省级专精特新企业" prop="enteIsProvinceSpecial">
            <el-radio-group v-model="showForm.enteIsProvinceSpecial" disabled>
              <el-radio
                v-for="dict in enteIsProvinceSpecialOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="国家级小型微型企业创业创新示范基地" prop="enteIsNationalInnovateExampleBase">
            <el-radio-group v-model="showForm.enteIsNationalInnovateExampleBase" disabled>
              <el-radio
                v-for="dict in enteIsNationalInnovateExampleBaseOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="省级小型微型企业创业创新示范基地" prop="enteIsProvinceInnovateExampleBase">
            <el-radio-group v-model="showForm.enteIsProvinceInnovateExampleBase" disabled>
              <el-radio
                v-for="dict in enteIsProvinceInnovateExampleBaseOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="国家级中小企业公共服务示范平台" prop="enteIsNationalPublicExamplePlatform">
            <el-radio-group v-model="showForm.enteIsNationalPublicExamplePlatform" disabled>
              <el-radio
                v-for="dict in enteIsNationalPublicExamplePlatformOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="省级中小企业公共服务示范平台" prop="enteIsProvincePublicExamplePlatform">
            <el-radio-group v-model="showForm.enteIsProvincePublicExamplePlatform" disabled>
              <el-radio
                v-for="dict in enteIsProvincePublicExamplePlatformOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="上市公司" prop="enteIsIpo">
            <el-radio-group v-model="showForm.enteIsIpo" disabled>
              <el-radio
                v-for="dict in enteIsIpoOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="enteRemark">
            <el-input v-model="showForm.enteRemark" type="textarea" placeholder="请输入内容" readonly/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="loading = false">取 消</el-button>
      </div>

    </el-dialog>
</template>

<script>
import { listEnterprise, getEnterprise } from "@/api/business/enterprise";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listArea} from "@/api/business/area";

export default {
  name: "Enterprise",
  dicts: ['sys_yes_no', 'sys_verify_type'],
  components: {
    Treeselect
  },
  data() {
    const enteIsNationalGiantCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否国家级小巨人企业"));
      } else {
        callback();
      }
    };
    const enteIsProvinceSpecialCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否省级专精特新企业"));
      } else {
        callback();
      }
    };
    const enteIsNationalInnovateExampleBaseCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否国家级小微企业创业创新示范基地"));
      } else {
        callback();
      }
    };
    const enteIsProvinceInnovateExampleBaseCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否省级小微企业创业创新示范基地"));
      } else {
        callback();
      }
    };
    const enteIsNationalPublicExamplePlatformCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否国家级中小企业公共服务示范平台"));
      } else {
        callback();
      }
    };
    const enteIsProvincePublicExamplePlatformCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否省级中小企业公共服务示范平台"));
      } else {
        callback();
      }
    };
    const enteIsIpoCheck = (rule, value, callback) => {
      if (value == 0) {
        callback(new Error("请选择是否上市公司"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 地区选项
      areaArrrayOptions: [],
      // 地区树选项
      areaOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 列表状态
      listState: "",
      // 总条数
      total: 0,
      // 企业表格数据
      enterpriseList: [],
      // 弹出层标题
      title: "",
      // 列表页面的功能按钮是否显示的变量
      handleAddShow: true,
      handleUpdateShow: true,
      handleDeleteShow: true,
      handleSubmitShow: true,
      handleApprovalShow: true,
      handleApprovalBackShow: true,
      // 审核对话框的审核、反审核按钮是否显示变量
      passButtonShow: true,
      backButtonShow: true,
      // 是否显示新增或修改企业信息的弹出层
      open: false,
      // 是否显示新增或修改企业信息的弹出层
      showopen: false,
      // 国家级小巨人企业字典
      enteIsNationalGiantOptions: [],
      // 省级专精特新企业字典
      enteIsProvinceSpecialOptions: [],
      // 国家级小型微型企业创业创新示范基地字典
      enteIsNationalInnovateExampleBaseOptions: [],
      // 省级小型微型企业创业创新示范基地字典
      enteIsProvinceInnovateExampleBaseOptions: [],
      // 国家中小企业公共服务示范平台字典
      enteIsNationalPublicExamplePlatformOptions: [],
      // 省级中小企业公共服务示范平台字典
      enteIsProvincePublicExamplePlatformOptions: [],
      // 上市公司字典
      enteIsIpoOptions: [],
      // 审批状态字典
      enteStateOptions: [],
      // 创建时间时间范围
      daterangeEnteCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enteId: null,
        enteTitle: null,
        enteCreditCode: null,
        enteEmail: null,
        enteArea: null,
        enteIsNationalGiant: null,
        enteIsProvinceSpecial: null,
        enteIsNationalInnovateExampleBase: null,
        enteIsProvinceInnovateExampleBase: null,
        enteIsNationalPublicExamplePlatform: null,
        enteIsProvincePublicExamplePlatform: null,
        enteIsIpo: null,
        enteCreateId: null,
        enteState: null,
        enteCreateTime: null,
        enteCreateBy: null,
        enteUpdateBy: null,
        enteApproveBy: null,
      },
      // 查看表单参数
      showForm: {},
      // 表单校验
      rules: {
        enteTitle: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        enteCreditCode: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" }
        ],
        enteBusinessLicense: [
          { required: true, message: "请上传营业执照", trigger: "blur" }
        ],
        enteEmail: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        enteArea: [
          { required: true, message: "所在地区不能为空", trigger: "change" }
        ],
        enteIsNationalGiant: [
          { required: true, message: "请选择是否国家级小巨人企业", trigger: "blur" },
          { required: true, validator: enteIsNationalGiantCheck, trigger: "blur" }
        ],
        enteIsProvinceSpecial: [
          { required: true, message: "请选择是否省级专精特新企业", trigger: "blur" },
          { required: true, validator: enteIsProvinceSpecialCheck, trigger: "blur" }
        ],
        enteIsNationalInnovateExampleBase: [
          { required: true, message: "请选择是否国家级小微企业创业创新示范基地", trigger: "blur" },
          { required: true, validator: enteIsNationalInnovateExampleBaseCheck, trigger: "blur" }
        ],
        enteIsProvinceInnovateExampleBase: [
          { required: true, message: "请选择是否省级小微企业创业创新示范基地", trigger: "blur" },
          { required: true, validator: enteIsProvinceInnovateExampleBaseCheck, trigger: "blur" }
        ],
        enteIsNationalPublicExamplePlatform: [
          { required: true, message: "请选择是否国家级中小企业公共服务示范平台", trigger: "blur" },
          { required: true, validator: enteIsNationalPublicExamplePlatformCheck, trigger: "blur" }
        ],
        enteIsProvincePublicExamplePlatform: [
          { required: true, message: "请选择是否省级中小企业公共服务示范平台", trigger: "blur" },
          { required: true, validator: enteIsProvincePublicExamplePlatformCheck, trigger: "blur" }
        ],
        enteIsIpo: [
          { required: true, message: "请选择是否上市公司", trigger: "blur" },
          { required: true, validator: enteIsIpoCheck, trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getAreaTreeselect();
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsNationalGiantOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsProvinceSpecialOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsNationalInnovateExampleBaseOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsProvinceInnovateExampleBaseOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsNationalPublicExamplePlatformOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsProvincePublicExamplePlatformOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.enteIsIpoOptions = response.data;
    });
    this.getDicts("sys_verify_type").then(response => {
      this.enteStateOptions = response.data;
    });
    this.getAreas().then(response => {
      this.areaArrrayOptions = response.data;
    });
  },
  methods: {
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeEnteCreateTime && '' != this.daterangeEnteCreateTime) {
        this.queryParams.params["beginEnteCreateTime"] = this.daterangeEnteCreateTime[0];
        this.queryParams.params["endEnteCreateTime"] = this.daterangeEnteCreateTime[1];
      }
      const listState = this.queryParams.enteState;
      listEnterprise(this.queryParams).then(response => {
        // 列表中每一行的按钮是否显示的变量
        this.handleAddShow = false;
        this.handleUpdateShow = false;
        this.handleDeleteShow = false;
        this.handleSubmitShow = false;
        this.handleApprovalShow = false;
        this.handleApprovalBackShow = false;

        if(listState == null || listState == "")
        {
          this.queryParams.enteState = "";
          this.handleAddShow = true;
          this.handleUpdateShow = true;
          this.handleDeleteShow = true;
        } else if(listState == "initial")
        {
          this.handleAddShow = true;
          this.handleUpdateShow = true;
          this.handleDeleteShow = true;
          this.handleSubmitShow = true;
        } else if(listState == "approvalback")
        {
          this.handleUpdateShow = true;
          this.handleDeleteShow = true;
          this.handleSubmitShow = true;
        } else if(listState == "approval")
        {
          this.handleApprovalShow = true;
        } else if(listState == "definite")
        {
          this.handleApprovalBackShow = true;
        }
        this.enterpriseList = response.rows;
        this.total = response.total;
      });
    },
    /** 转换地区数据结构 */
    areaNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaTitle,
        children: node.children
      };
    },
    /** 查询地区下拉树结构 */
    getAreaTreeselect() {
      listArea().then(response => {
        this.areaOptions = [];
        const data = { areaId: 0, areaTitle: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "areaId", "areaFatherId");
        this.areaOptions.push(data);
      });
    },
    /** 创建列表中地区名字的字典翻译 */
    enteAreaFormat(row, column) {
      return this.selectAreaLabel(this.areaArrrayOptions, row.enteArea)
    },
    // 国家级小巨人企业字典翻译
    enteIsNationalGiantFormat(row, column) {
      return this.selectDictLabel(this.enteIsNationalGiantOptions, row.enteIsNationalGiant);
    },
    // 省级专精特新企业字典翻译
    enteIsProvinceSpecialFormat(row, column) {
      return this.selectDictLabel(this.enteIsProvinceSpecialOptions, row.enteIsProvinceSpecial);
    },
    // 是否国家级小型微型企业创业创新示范基地字典翻译
    enteIsNationalInnovateExampleBaseFormat(row, column) {
      return this.selectDictLabel(this.enteIsNationalInnovateExampleBaseOptions, row.enteIsNationalInnovateExampleBase);
    },
    // 省级小型微型企业创业创新示范基地字典翻译
    enteIsProvinceInnovateExampleBaseFormat(row, column) {
      return this.selectDictLabel(this.enteIsProvinceInnovateExampleBaseOptions, row.enteIsProvinceInnovateExampleBase);
    },
    // 国家中小企业公共服务示范平台字典翻译
    enteIsNationalPublicExamplePlatformFormat(row, column) {
      return this.selectDictLabel(this.enteIsNationalPublicExamplePlatformOptions, row.enteIsNationalPublicExamplePlatform);
    },
    // 省级中小企业公共服务示范平台字典翻译
    enteIsProvincePublicExamplePlatformFormat(row, column) {
      return this.selectDictLabel(this.enteIsProvincePublicExamplePlatformOptions, row.enteIsProvincePublicExamplePlatform);
    },
    // 上市公司字典翻译
    enteIsIpoFormat(row, column) {
      return this.selectDictLabel(this.enteIsIpoOptions, row.enteIsIpo);
    },
    // 审批状态字典翻译
    enteStateFormat(row, column) {
      return this.selectDictLabel(this.enteStateOptions, row.enteState);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.showopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.showForm = {
        enteId: null,
        enteTitle: null,
        enteCreditCode: null,
        enteBusinessLicense: null,
        enteEmail: null,
        enteArea: null,
        enteAreaInfo: null,
        enteIsNationalGiant: "0",
        enteIsProvinceSpecial: "0",
        enteIsNationalInnovateExampleBase: "0",
        enteIsProvinceInnovateExampleBase: "0",
        enteIsNationalPublicExamplePlatform: "0",
        enteIsProvincePublicExamplePlatform: "0",
        enteIsIpo: "0",
        enteRemark: null,
        enteCreateId: null,
        enteState: null,
        enteCreateTime: null,
        enteUpdateId: null,
        enteCreateBy: null,
        enteUpdateTime: null,
        enteApproveId: null,
        enteApproveTime: null,
        enteUpdateBy: null,
        enteApproveBy: null
      };
      this.resetForm("showForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEnteCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.enteId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleShow(row) {
      this.reset();
      this.getAreaTreeselect();
      const enteId = row.enteId || this.ids
      getEnterprise(enteId).then(response => {
        this.showForm = response.data;
        this.showopen = true;
        this.title = "查看企业";
        this.passButtonShow = false;
        this.backButtonShow = false;
      });
    }
  }
};
</script>
